package jp.co.toshiba.ffsa.action.csct;

import java.util.ArrayList;

import javax.annotation.Resource;

import jp.co.toshiba.ffsa.action.common.CommonAction;
import jp.co.toshiba.ffsa.dto.LoginDto;
import jp.co.toshiba.ffsa.entity.User;
import jp.co.toshiba.ffsa.form.csct.Csct101Form;
import jp.co.toshiba.ffsa.service.common.ConstantValue;
import jp.co.toshiba.ffsa.service.csct.Csct101Service;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ActionMessagesUtil;
import org.seasar.struts.util.MessageResourcesUtil;
import org.seasar.struts.util.RequestUtil;

/**
 * お問い合わせ内容入力
 * @author npc
 */
public class Csct101Action extends CommonAction {
    /** 画面ID */
    public String screenID = "CSCT101";

    /** ActionForm */
    @ActionForm
    @Resource
    protected Csct101Form csct101Form;

    @Resource
    protected Csct101Service csct101Service;

    /**
     * 初期処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = false)
    public String index() throws Exception {
        // ログイン済判定する
        if(this.judgeLogin(csct101Form.getSessionId())){
            try {
                // リファラ判定
                if(this.judgeRefferer()){

                    // Serviceクラスを呼び出す
                    Csct101Service service = new Csct101Service();
                    service.setUserId(((LoginDto)RequestUtil.getRequest().getSession().getAttribute("login_dto")).getUserID());
                    User getUser = service.getUser();

                    // ユーザーが存在した場合
                    if(getUser != null){
                        this.csct101Form.emailAddress = getUser.mailAddress;
                    }else{
                        // ユーザーが存在しない場合
                        ActionMessages messages = new ActionMessages();
                        messages.add("userHaveExisted", new ActionMessage("errors.exist",MessageResourcesUtil.getMessage("mail_address"),true));
                        ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                    }

                    return "/csct/csct101.jsp";
                }else{
                    return this.topTransition;
                }
            } catch (Exception e) {
                this.errorLogWriter(e, "index", "CSCT101");
                throw e;
            }
        }else{
            return this.topTransition;
        }
    }
    /**
     * 確認処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = true,input="/csct/csct101")
    public String check() throws Exception {
        String transition = "/csct/csct101";
        String result = "";
        // ログイン済判定する
        if(this.judgeLogin(csct101Form.getSessionId())){
            try {
                // リファラ判定
                if(this.judgeRefferer()){
                    csct101Service = new Csct101Service();
                    csct101Service.setEmailAddress(this.csct101Form.emailAddress);
                    csct101Service.setSubject(this.csct101Form.subject);
                    csct101Service.setBody(this.csct101Form.body);
                    csct101Service.setUserId(((LoginDto)RequestUtil.getRequest().getSession().getAttribute("login_dto")).getUserID());
                    // メール送信
                    if(csct101Service.mailSend()){
                        transition = "/csct/csct102.jsp";
                        result = ConstantValue.success;
                    } else {
                        ActionMessages messages = new ActionMessages();
                        messages.add("mailSendFailed", new ActionMessage(csct101Service.getErrorCode(), true));
                        ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                        transition = "/csas/csct101.jsp";
                        result = ConstantValue.fail;
                    }
                    if(ConstantValue.success.equals(result)){
                        // 操作ログ記述
                        ArrayList<ArrayList<String>> parameters = new ArrayList<ArrayList<String>>();
                        ArrayList<String> parameterNames = new ArrayList<String>();
                        ArrayList<String> values = new ArrayList<String>();
                        parameterNames.add(MessageResourcesUtil.getMessage("subject"));
                        values.add(this.csct101Form.subject);
                        parameterNames.add(MessageResourcesUtil.getMessage("inquiry"));
                        values.add(this.csct101Form.body);
                        parameters.add(parameterNames);
                        parameters.add(values);
                        this.entryOperationHistory(this.screenID, "submit", parameters, result);
                    }
                }else{
                    return this.topTransition;
                }
            }catch(Exception e) {
                this.errorLogWriter(e, "check", "CSCT101");
                throw e;
            }
        }else{
            return this.topTransition;
        }
        return transition;
    }
}